package com.jf.dao.impl;

import com.jf.bean.Token;
import com.jf.bean.Userinfo;
import com.jf.dao.TokenDao;
import com.jf.util.BaseDao;

public class TokenDaoImpl implements TokenDao {
    @Override
    public int add(Token token) {
        String sql = "insert into token values(null,?,?,?)";
        return BaseDao.baseUpdate(sql,token.getLoginId(),token.getExpires(),token.getToken());
    }

    @Override
    public Token getByToken(String token) {
        return BaseDao.baseQueryBean(Token.class,"select * from token where token = ?",token);
    }

    @Override
    public Token getByUserId(int userinfoId) {
        return BaseDao.baseQueryBean(Token.class,"select * from token where login_id = ?",userinfoId);
    }

    @Override
    public int update(Token token) {
        String sql = "update token set login_id = ?,token = ?,expires=? where id = ?";
        return BaseDao.baseUpdate(sql,token.getLoginId(),token.getToken(),token.getExpires(),token.getId());
    }

    @Override
    public int delete(String token) {
        String sql = "delete from token where token = ?";
        return BaseDao.baseUpdate(sql,token);
    }

    @Override
    public Userinfo getUserByToken(String token) {
        String sql = "select u.* from token t left join userinfo u on u.id = t.login_id where token = ?";
        return BaseDao.baseQueryBean(Userinfo.class,sql,token);
    }
}
